
package portlet;

import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.PortletConfig;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

import javax.portlet.PortletRequestDispatcher;

import org.apache.log4j.Logger;

import core.ConfigUtil;
import core.corewa.CoreWa;
import core.corewa.Event;
import core.corewa.Screen;
import db.dto.WordsDTO;
import form.BaseForm;
import form.ReviewerCheckingForm;

/**
 * OnlineStardictPortlet Portlet Class
 * @author disable
 */
public class OnlineStardictPortlet extends GenericPortlet {
	
	Logger LOG = Logger.getLogger("OnlineStardictPortlet");
	
	String contextPath;
	CoreWa coreWebAppCfg = null;
	Screen screen = null;
	Event event = null;
	BaseForm result;
	
	@Override
	public void init(PortletConfig portletCfg) throws PortletException {
		super.init(portletCfg);
		contextPath = portletCfg.getInitParameter("contextPath");
		String mvcPortletCfg = portletCfg.getInitParameter("Configure");
		String realPathCfg = portletCfg.getPortletContext().getRealPath(mvcPortletCfg);
		coreWebAppCfg = ConfigUtil.parse(realPathCfg);
	}


	public void doView(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");
		
	    PortletRequestDispatcher dispatcher = null;
	    if(event == null) {
	    	request.getPortletSession().removeAttribute("username", PortletSession.APPLICATION_SCOPE);
			request.getPortletSession().removeAttribute("role", PortletSession.APPLICATION_SCOPE);
	    	request.getPortletSession().setAttribute("username", "Guest",PortletSession.APPLICATION_SCOPE);
	    	request.getPortletSession().setAttribute("role", -1, PortletSession.APPLICATION_SCOPE);
	    	dispatcher = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/SearchWord.jsp"); 
	    } else {
//	    	if(result instanceof ReviewerCheckingForm) {
//	    		ReviewerCheckingForm _result = (ReviewerCheckingForm) result;
//	    		LOG.info("--------");
//	    		LOG.info("dictName :"+_result.getDictName());
//	    		List<WordsDTO> listWord = _result.getListNewWords();
//	    		for (WordsDTO wordsDTO : listWord) {
//					LOG.info("--begin--");
//					LOG.info("word :"+wordsDTO.getWord());
//					LOG.info("meaning :"+wordsDTO.getMeaning());
//					LOG.info("--end--");
//				}
//	    		LOG.info(event.getNextScrId());
//	    		LOG.info("--------");
//	    	}
	    	dispatcher = getPortletContext().getRequestDispatcher(event.getNextScrId());
	    }
	    request.setAttribute("result", result);
	    request.setAttribute("contextPath", contextPath);
	    dispatcher.include(request, response);
	}


	public void doEdit(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");
		
        PortletRequestDispatcher dispatcher =
	        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/SearchWord.jsp");
        dispatcher.include(request, response);
		
	}

	public void doHelp(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {

		response.setContentType("text/html");
		
        PortletRequestDispatcher dispatcher =
	        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/SearchWord.jsp");
        dispatcher.include(request, response);
		
	}

	public void processAction(ActionRequest request, ActionResponse response)
			throws PortletException, IOException {
		String screenId = request.getParameter("screenId");
		String eventId = request.getParameter("eventId");
		screen = coreWebAppCfg.getScreen(screenId);
		event = screen.getEvent(eventId);
		try {
			Method method;
			Object controlClass = Class.forName(screen.getCtrlClass())
					.newInstance();
			method = controlClass.getClass().getMethod(event.getProcId(),
					ActionRequest.class, ActionResponse.class, Event.class);
			result = (BaseForm) method.invoke(controlClass, request, response, event);
		} catch (Exception ex) {
		}
	}

}
